package com.tlgen.orm.support;

import com.tlgen.orm.param.QueryParams;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

import static com.tlgen.orm.enums.Query.*;

/**
 * 查询工厂
 */
public class QueryFactory {

    private static final Map<String, Function<QueryParams, String>> map = new ConcurrentHashMap<>();

    static {
        map.put(LIKE.getValue(), OperatorFactory::like);
        map.put(NOT_LIKE.getValue(), OperatorFactory::notLike);
        map.put(LIKE_LEFT.getValue(), OperatorFactory::likeLeft);
        map.put(LIKE_RIGHT.getValue(), OperatorFactory::likeRight);
        map.put(NE.getValue(), OperatorFactory::ne);
        map.put(OR.getValue(), OperatorFactory::or);
        map.put(EQ.getValue(), OperatorFactory::eq);
        map.put(IN.getValue(), OperatorFactory::in);
        map.put(NOT_IN.getValue(), OperatorFactory::notIn);
        map.put(LT.getValue(), OperatorFactory::lt);
        map.put(LE.getValue(), OperatorFactory::le);
        map.put(GT.getValue(), OperatorFactory::gt);
        map.put(GE.getValue(), OperatorFactory::ge);
        map.put(BETWEEN.getValue(), OperatorFactory::between);
        map.put(NOT_BETWEEN.getValue(), OperatorFactory::notBetween);
        map.put(IS_NULL.getValue(), OperatorFactory::isNull);
        map.put(IS_NOT_NULL.getValue(), OperatorFactory::isNotNull);
        map.put(GROUP_BY.getValue(), OperatorFactory::groupBy);
        map.put(ORDER_BY_ASC.getValue(), OperatorFactory::orderByAsc);
        map.put(ORDER_BYD_ESC.getValue(), OperatorFactory::orderByDesc);
    }

    /**
     * 普通查询映射操作
     *
     */
    public static String mappingOperator(List<QueryParams> queryParamsList) {
        List<String> stringList = new ArrayList<>();
        queryParamsList.forEach(x -> stringList.add(map.get(x.getOperator()).apply(x)));
        return String.join("", stringList);
    }

}
